﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using OfficialWeb.Application;

using OOS.Application.Cart.Dtos;
using OOS.Application.Cart.Services.Interfaces;
using OOS.Application.Dish.Services.Interfaces;
using OOS.Application.Setmeal.Services.Interfaces;
using OOS.Core.Cart;

namespace OOS.Application.Cart.Services
{
    /// <summary>
    /// 购物车
    /// </summary>
    public class ShoppingCartService: IShoppingCartService,ITransient
    {
        private readonly ISqlSugarClient _client;

        private readonly IDishBasicService _dishBasicService;

        private readonly ISetmealBasicService _setmealBasicService;

        public ShoppingCartService(ISqlSugarClient client, IDishBasicService dishBasicService, ISetmealBasicService setmealBasicService)
        {
            _client = client;
            _dishBasicService = dishBasicService;
            _setmealBasicService = setmealBasicService;
        }

        /// <summary>
        /// 新增购物车数据并返回购物车数据id
        /// </summary>
        /// <param name="shoppingCart"></param>
        /// <returns></returns>
        public int InsertShoppingCart(ShoppingCart shoppingCart)
        {
            if (shoppingCart == null) { return 0; }
            //判断token中是否有用户数据
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return 0; }
            shoppingCart.UserId = userId;
            shoppingCart.TotalMoney = shoppingCart.TotalMoney * shoppingCart.Num;
            shoppingCart.CreateDate = DateTime.Now;
            shoppingCart.UpdateDate = DateTime.Now;
            shoppingCart.IsDeleted = false;
            return _client.Insertable(shoppingCart).ExecuteReturnIdentity();
        }

        /// <summary>
        /// 根据id查询购物车数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ShoppingCartDto SelectShoppingCartById(int id)
        {
            if (id <= 0) return new ShoppingCartDto();
            ShoppingCart shoppingCart = this.SelectCartById(id);
            int dishId = shoppingCart.DishId;
            int setmealId = shoppingCart.SetmealId;
            ShoppingCartDto shoppingCartDto = shoppingCart.Adapt<ShoppingCartDto>();
            if (dishId > 0)
            {
                shoppingCartDto.dishBasicDto = _dishBasicService.SelectDishById(dishId);
            }
            if (setmealId > 0)
            {
                shoppingCartDto.setmealBasicDto = _setmealBasicService.SelectSetmealById(setmealId);
            }
            return shoppingCartDto;
        }

        /// <summary>
        /// 根据id查询购物车数据库原始数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ShoppingCart SelectCartById(int id)
        {
            return _client.Queryable<ShoppingCart>()
                .Where(sc => sc.Id == id)
                .Where(sc => sc.IsDeleted == false)
                .First();
        }

        /// <summary>
        /// 通过token中的用户id获取对应的购物车列表
        /// </summary>
        /// <returns></returns>
        public List<ShoppingCartDto> SelectShoppingCartDtosByUserId()
        {
            int userId = JWTToken.GetUser().Id;
            if (userId <= 0) { return new List<ShoppingCartDto>(); }
            List<ShoppingCartDto> shoppingCartDtos = _client.Queryable<ShoppingCart>()
                .Where(sc => sc.IsDeleted == false)
                .Where(sc => sc.UserId == userId)
                .OrderBy(sc => sc.CreateDate,OrderByType.Desc)
                .ToList()
                .Adapt<List<ShoppingCartDto>>();
            for (int i = 0; i < shoppingCartDtos.Count; i++)
            {
                shoppingCartDtos[i] = this.SelectShoppingCartById(shoppingCartDtos[i].Id);
            }
            return shoppingCartDtos;
        }

        /// <summary>
        /// 根据id修改对应购物车数据
        /// </summary>
        /// <param name="shoppingCart"></param>
        /// <returns></returns>
        public bool UpdateShoppingCartById(ShoppingCart shoppingCart)
        {
            if (shoppingCart == null) { return false; }
            ShoppingCart cart = this.SelectCartById(shoppingCart.Id);
            cart.Num = shoppingCart.Num;
            cart.UpdateDate = DateTime.Now;
            cart.IsDeleted = shoppingCart.IsDeleted;
            return _client.Updateable(cart).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 根据id列表批量删除购物车数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteShoppingCartByIds(int[] ids)
        {
            if (ids == null || ids.Length == 0) {  return false; }
            foreach (var id in ids)
            {
                ShoppingCart cart = this.SelectCartById(id);
                cart.IsDeleted = true;
                Boolean flag = this.UpdateShoppingCartById(cart);
                if(!flag) { return false; }
            }
            return true;
        }
    }
}
